//
//  UserCollectionViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/2/10.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "UserCollectionViewController.h"
#import "RHTableView.h"
#import "NewsTableViewCell.h"
#import "InsuranceBrokersContentViewController.h"
#import "LicensePlateTableViewCell.h"
#import "ProductKeysView.h"
#import "V4TestLicensePlateContentViewController.h"
#import "NavigationBarToggleTabView.h"


@interface UserCollectionViewController ()<UITextFieldDelegate,ProductKeysViewDelegate>{
   // UITextField *txtSarch;
    RHTableView *tabelViewC;
    
    
    UIView *viewLine;
    RHTableView *tabelViewNews;
    
    ProductKeysView *productKey;
    
    //---------------v4--------------
    NavigationBarToggleTabView *toggleTab;
    NSString *strType;
    
}

@end

@implementation UserCollectionViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self refreshTableData];
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navbarTitle:@" " ];
    
    RegisterNotify(@"againLoadUserData", @selector(againLoadUserData))
    strType=@"1";
  
    float fty=kTopHeight;//108;
    
    tabelViewC=[[RHTableView alloc]initWithFrame:CGRectMake(0, fty, kScreenWidth, H(self.view)-fty)];
    [tabelViewC showRefresh:YES LoadMore:YES];
    tabelViewC.delegate = self;
    tabelViewC.dataSource = self;
    //    tabelViewC.delegate2=self;
    [tabelViewC setBackgroundColor:rgbGray];
    [tabelViewC setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [tabelViewC registerClass:[LicensePlateTableViewCell class] forCellReuseIdentifier:@"LicensePlateTableViewCell"];
    [tabelViewC setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    
    [self.view addSubview:tabelViewC];
    
    
    tabelViewNews=[[RHTableView alloc]initWithFrame:tabelViewC.frame];
    [tabelViewNews showRefresh:YES LoadMore:YES];
    tabelViewNews.delegate = self;
    tabelViewNews.dataSource = self;
    //    tabelViewNews.delegate2=self;
    [tabelViewNews setBackgroundColor:rgbGray];
    [tabelViewNews setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [tabelViewNews registerClass:[NewsTableViewCell class] forCellReuseIdentifier:@"NewsTableViewCell"];
    [tabelViewNews setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    tabelViewNews.hidden=YES;
    [self.view addSubview:tabelViewNews];
    
    [self initNavTopButtons];
    
}
-(void)againLoadUserData{
    [self refreshTableData];
}
-(void)refreshTableData{
    if ([strType isEqualToString:@"1"]) {
        tabelViewC.urlString=[NSString stringWithFormat:XKFproductsmycollection,[[Utility Share] userId],[[Utility Share] userToken],@"",@"",@"%@",@""];
        [tabelViewC refresh];
        
    }else {
        tabelViewNews.urlString=[NSString stringWithFormat:XKFnewsmycollection,[[Utility Share] userId],[[Utility Share] userToken],@"",@"",@"%@"];
        [tabelViewNews refresh];
    }
    
}
-(void)initNavTopButtons{
    toggleTab=[[NavigationBarToggleTabView alloc] initWithFrame:CGRectMake((kScreenWidth-150)/2, 28, 150, 28)];
    [self.navView addSubview:toggleTab];
    
    [self refreshNavTopButtonsData];
}
-(void)refreshNavTopButtonsData{
    __weak UserCollectionViewController *viewC=self;
    __weak RHTableView *weakTable1=tabelViewC;
    __weak RHTableView *weakTable2=tabelViewNews;
    [toggleTab setTiltelArray:@[@{@"title":@"牌照产品",@"id":@"1"},@{@"title":@"新闻快讯",@"id":@"2"}] toggleTab:^(NSDictionary *dicSelect) {
        if ([[dicSelect valueForJSONStrKey:@"id"] isEqualToString:@"1"]) {
            weakTable2.hidden=YES;
            weakTable1.hidden=NO;
        }else{
            weakTable1.hidden=YES;
            weakTable2.hidden=NO;
        }
        strType=[dicSelect valueForJSONStrKey:@"id"];
        [viewC refreshTableData];
    }];
}

#pragma mark button

#pragma mark tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==tabelViewC) {
        return tabelViewC.dataArray.count;
    }else{
        return tabelViewNews.dataArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView==tabelViewC) {
        NSDictionary *dic=[tabelViewC.dataArray objectAtIndex:indexPath.row];
        float fth=[self heightForLabel:kScreenWidth-50 font:fontTitle text:[dic valueForJSONStrKey:@"stitle"]];
        fth = fth>20?40:20;
        return fth+65;
    }else{
        return 95;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==tabelViewC) {
        NSDictionary *dic=[tabelViewC.dataArray objectAtIndex:indexPath.row];
        LicensePlateTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LicensePlateTableViewCell" forIndexPath:indexPath];
        [cell setValueForDictionary:dic indexPath:indexPath type:@"user"];
        return cell;
    }else{
        NSDictionary *dic=[tabelViewNews.dataArray objectAtIndex:indexPath.row];
        NewsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"NewsTableViewCell" forIndexPath:indexPath];
        [cell setValueForDictionary:dic indexPath:indexPath type:@"Experts"];
        return cell;
    }
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  //  [txtSarch resignFirstResponder];
    
    if (tableView==tabelViewC) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSDictionary *dic=[tabelViewC.dataArray objectAtIndex:indexPath.row];
        [self pushController:[V4TestLicensePlateContentViewController class] withInfo:@"Insurance" withTitle:@"牌照详情" withOther:dic];
    }else{
        NSDictionary *dic=[tabelViewNews.dataArray objectAtIndex:indexPath.row];
        [self pushController:[InsuranceBrokersContentViewController class] withInfo:@"new" withTitle:@" " withOther:dic];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
